package cz.ujep.ki.emap;

import java.util.Random;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class EMapActivity extends Activity implements OnClickListener, LocationListener {
	//views
	private Button like;
	private Button dislike;
	private Spinner category;

	//GPS
	private boolean likeClicked;
	private LocationManager locationManager;
	private String provider;

	//generation of session id
	private Random rgen = new Random();
	private long session_id = rgen.nextLong();	
	private final long sessionDelay = 15 *1000;
	private android.os.Handler sessionHandler = new android.os.Handler();
	
	Runnable sessionChanger = new Runnable() {
		public void run() {
			session_id = rgen.nextLong();
			sessionHandler.postDelayed(sessionChanger, sessionDelay);
		}
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //získání odkazů na views
        like=(Button)findViewById(R.id.button1);
        dislike=(Button)findViewById(R.id.button2);
        category=(Spinner)findViewById(R.id.spinner1);
        
        //naplnění spinneru (rozbalovacího seznamu)
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.Categories, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        category.setAdapter(adapter);
        
        like.setOnClickListener(this);
        dislike.setOnClickListener(this);
        sessionHandler.postDelayed(sessionChanger, sessionDelay);
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    	Criteria criteria = new Criteria();
    	provider = locationManager.getBestProvider(criteria, true);
    }
    
	public void onClick(View v) {
		likeClicked = v == like;
		//nastartování 
		locationManager.requestLocationUpdates(provider, 0, 0, this);
		//zešedění tlačítek
		like.setEnabled(false);
		dislike.setEnabled(false);
	}

	public void onLocationChanged(Location location) {
        //opětná aktivace tlačítek
		like.setEnabled(true);
		dislike.setEnabled(true);
		locationManager.removeUpdates(this);
		TopoEmotion msg = new TopoEmotion(session_id, location.getLongitude(), 
				                      location.getLatitude(),
				                      category.getSelectedItemPosition(), likeClicked,
				                      location.getAccuracy());
		Intent intent = new Intent(this, DeliverService.class);
		try {
			intent.putExtra("message", msg.toJSON().toString());
		} catch (JSONException e) {
			e.printStackTrace();
			return;
		}
		startService(intent);
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
	}

}